using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Interfaces;
using Microsoft.Xna.Framework;

namespace Perovich.GameObjects.TwoDimensional
{
    /// <summary>
    /// The <see cref="Projectile2D"/> class is an <see cref="InteractiveObject2D"/> that automaticly checks collisions with all of its sibling <see cref="InteractiveObject2D"/>s.
    /// </summary>
    public class Projectile2D : InteractiveObject2D
    {
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Projectile2D"/> generates collision events with other <see cref="Projectile2D"/>s.
        /// </summary>
        /// <value>true to generate collisions; false otherwise.</value>
        public bool CollidesWithProjectiles
        {
            get;
            set;
        }

        /// <summary>
        /// Allows the <see cref="Projectile2D"/> to check for collisions.
        /// </summary>
        protected override void CheckCollisionsCore()
        {
            if (Active)
            {
                foreach (object comp in this.Siblings)
                {
                    InteractiveObject2D otherObject = comp as InteractiveObject2D;
                    if (otherObject != null)
                    {
                        if ((!(otherObject is Projectile2D)) || (CollidesWithProjectiles))
                        {
                            if (CollisionBox.Intersects(otherObject.CollisionBox))
                            {
                                SignalCollision(otherObject);
                            }
                        }
                    }
                }
            }
            base.CheckCollisionsCore();
        }


        /// <summary>
        /// Constructs a new instance of the <see cref="Projectile2D"/> class.
        /// </summary>
        /// <param name="game"><see cref="Perovich.GameObjects.Interfaces.ICanHoldComponents"/> instance that this <see cref="Projectile2D"/> will be constructed for.</param>
        public Projectile2D(ICanHoldComponents game)
            : base(game)
        {
            CollidesWithProjectiles = false;
        }

        /// <summary>
        /// Allows the <see cref="Projectile2D"/> to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of the game's timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            Rotation = Perovich.GameObjects.ComponentsExtensions.GetDirection(Velocity);
        }
    }
}
